import api, { APIResponse } from './api'
import { Folder, CreateFolderData, UpdateFolderData, FolderContents, FolderStats } from '@/types/folder'

// 获取文件夹树
export const getFolderTree = (): Promise<APIResponse<Folder[]>> => {
  return api.get('/folders/tree').then(res => res.data)
}

// 获取子文件夹
export const getChildFolders = (parentId?: string): Promise<APIResponse<{
  folders: Folder[]
  total: number
}>> => {
  return api.get('/folders', {
    params: { parent_id: parentId }
  }).then(res => res.data)
}

// 搜索文件夹
export const searchFolders = (keyword: string, params?: {
  parent_id?: string
  created_by?: string
  limit?: number
  offset?: number
}): Promise<APIResponse<{
  folders: Folder[]
  total: number
}>> => {
  return api.get('/folders', {
    params: { keyword, ...params }
  }).then(res => res.data)
}

// 获取单个文件夹
export const getFolder = (id: string): Promise<APIResponse<Folder>> => {
  return api.get(`/folders/${id}`).then(res => res.data)
}

// 创建文件夹
export const createFolder = (data: CreateFolderData): Promise<APIResponse<Folder>> => {
  return api.post('/folders', data).then(res => res.data)
}

// 更新文件夹
export const updateFolder = (id: string, data: UpdateFolderData): Promise<APIResponse<Folder>> => {
  return api.put(`/folders/${id}`, data).then(res => res.data)
}

// 删除文件夹
export const deleteFolder = (id: string, force = false): Promise<APIResponse<void>> => {
  return api.delete(`/folders/${id}`, {
    params: { force }
  }).then(res => res.data)
}

// 获取文件夹内容
export const getFolderContents = (id: string): Promise<APIResponse<FolderContents>> => {
  return api.get(`/folders/${id}/contents`).then(res => res.data)
}

// 移动文件夹
export const moveFolder = (id: string, parentId?: string): Promise<APIResponse<Folder>> => {
  return api.patch(`/folders/${id}/move`, {
    parent_id: parentId
  }).then(res => res.data)
}

// 重命名文件夹
export const renameFolder = (id: string, name: string): Promise<APIResponse<Folder>> => {
  return api.patch(`/folders/${id}/rename`, { name }).then(res => res.data)
}

// 复制文件夹
export const copyFolder = (id: string, data: {
  name: string
  parent_id?: string
  created_by?: string
}): Promise<APIResponse<Folder>> => {
  return api.post(`/folders/${id}/copy`, data).then(res => res.data)
}

// 获取文件夹路径
export const getFolderPath = (id: string): Promise<APIResponse<{ path: string }>> => {
  return api.get(`/folders/${id}/path`).then(res => res.data)
}

// 获取文件夹统计
export const getFolderStats = (id: string): Promise<APIResponse<FolderStats>> => {
  return api.get(`/folders/${id}/stats`).then(res => res.data)
}

// 批量操作文件夹
export const batchOperateFolders = (data: {
  folder_ids: string[]
  operation: 'delete' | 'move'
  target_parent_id?: string
  force?: boolean
}): Promise<APIResponse<{
  success_count: number
  failed_count: number
  errors: string[]
}>> => {
  return api.post('/folders/batch', data).then(res => res.data)
}

// 获取文件夹权限
export const getFolderPermissions = (id: string): Promise<APIResponse<{
  folder_id: string
  owner: string
  permissions: {
    read: boolean
    write: boolean
    delete: boolean
    manage: boolean
  }
  shared_with: string[]
  created_at: string
}>> => {
  return api.get(`/folders/${id}/permissions`).then(res => res.data)
}

// 更新文件夹权限
export const updateFolderPermissions = (id: string, data: {
  permissions?: {
    read: boolean
    write: boolean
    delete: boolean
    manage: boolean
  }
  shared_with?: string[]
}): Promise<APIResponse<{
  folder_id: string
  permissions: {
    read: boolean
    write: boolean
    delete: boolean
    manage: boolean
  }
  shared_with: string[]
  updated_at: string
}>> => {
  return api.put(`/folders/${id}/permissions`, data).then(res => res.data)
}